package org.zsk.cfl.listerner;

import lombok.extern.slf4j.Slf4j;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * <p>
 * 多实例任务监听器
 * </p>
 *
 * @author zsk
 * @date 2020/11/10 上午8:18
 */
@Slf4j
@Component("multiTaskListener")
public class MultiTaskListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        //当次投票结果,默认弃权
        String vote = (String) Optional.ofNullable(delegateTask.getVariable("vote")).orElse(MultiConstants.ABSTAINED);
        log.debug("{} 投票：{}", delegateTask.getAssignee(), vote);
        //ExecutionListner类中设置的同意、拒绝、弃权计数变量
        int acceptedCount = (int) delegateTask.getVariable(MultiConstants.ACCEPTED);
        int rejectedCount = (int) delegateTask.getVariable(MultiConstants.REJECTED);
        int abstainedCount = (int) delegateTask.getVariable(MultiConstants.ABSTAINED);
        switch (vote) {
            case MultiConstants.REJECT:
                //拒绝
                delegateTask.setVariable(MultiConstants.REJECTED, ++rejectedCount);
                break;
            case MultiConstants.ACCEPT:
                //同意
                delegateTask.setVariable(MultiConstants.ACCEPTED, ++acceptedCount);
                break;
            case MultiConstants.ABSTAIN:
                //弃权
                delegateTask.setVariable(MultiConstants.ABSTAINED, ++abstainedCount);
                break;
            default:
                log.debug("投票异常");
        }
        log.debug("同意票-{} 反对票-{} 弃权票-{}", acceptedCount, rejectedCount, abstainedCount);
    }
}
